package com.YxyBackMange.Controller.web;

import com.YxyBackMange.Controller.SystemController.QueryController;
import com.YxyBackMange.Dto.PageInfo;
import com.YxyBackMange.Dto.QueryInfo;
import com.YxyBackMange.Entity.App.AppBanner;
import com.YxyBackMange.Entity.App.AppNews;
import com.YxyBackMange.Entity.Ware.WareGoodsInfo;
import com.YxyBackMange.Entity.Ware.WareGoodsQuery;
import com.YxyBackMange.Mapper.web.WebAppBannerMapper;
import com.YxyBackMange.Mapper.web.WebGoodsMapper;
import com.YxyBackMange.Mapper.web.WebNewsMapper;
import com.YxyBackMange.Service.web.WebGoodsService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/12/5.
 */
@Controller
@RequestMapping("/Web/WebGoodsInfo")
public class WebGoodsController {

    @Autowired
    private WebGoodsService webGoodsService;
    @Autowired
    private WebGoodsMapper webGoodsMapper;
    @Autowired
    private WebNewsMapper webNewsMapper;

    @RequestMapping("/findAllGoodsInfo")
    public String findAllGoodsInfo(HttpSession session, HttpServletRequest request, ModelMap map){
        List<WareGoodsInfo> allGoodsInfo = webGoodsService.findAllGoodsInfo();
        Integer i=0;
        Integer j=0;
        List<WareGoodsInfo> nominateList = new ArrayList<WareGoodsInfo>();
        List<WareGoodsInfo> specialList = new ArrayList<WareGoodsInfo>();
        List<WareGoodsInfo> nowPriceList = new ArrayList<WareGoodsInfo>();
        for (WareGoodsInfo goodsInfo : allGoodsInfo) {
            if (allGoodsInfo.size()<6) {
                if (i<3){
                    if (goodsInfo.getIsNominate()==1){
                        nominateList.add(goodsInfo);
                        i++;
                    }
                }else {
                    break;
                }
            }else{
                if (i<6){
                    if (goodsInfo.getIsNominate()==1){
                        nominateList.add(goodsInfo);
                        i++;
                    }
                }else {
                    break;
                }
            }
        }
        for (WareGoodsInfo goodsInfo : allGoodsInfo){
            if (j++<3){
                specialList.add(goodsInfo);
            }else {
                break;
            }
        }
        map.put("nominateList",nominateList);
        session.setAttribute("specialList",specialList);

        //价格优惠商品
        QueryInfo queryInfoPrice = new QueryInfo();
        queryInfoPrice.setSort("nowPrice");
        queryInfoPrice.setOrder("");
        queryInfoPrice.setPageSize(3);
        PageInfo wareGoodsInfo = new QueryController().query(queryInfoPrice, "WareGoodsInfo", session, request);
        System.out.println("queryInfoPrice:"+wareGoodsInfo.getRows().size());
        map.put("queryInfoPrice",wareGoodsInfo.getRows());
        //轮播图查询
        QueryInfo queryInfo = new QueryInfo();
        queryInfo.setSort("createTime");
        queryInfo.setPageSize(3);
        PageInfo appBanner = new QueryController().query(queryInfo, "AppBanner", session, request);
        System.out.println("Banner:"+appBanner.getRows().size());
        map.put("appBannerList",appBanner.getRows());
        //新闻
        QueryInfo newQueryInfo = new QueryInfo();
        newQueryInfo.setPageSize(4);
        newQueryInfo.setSort("createTime");
        PageInfo newinfo = new QueryController().query(newQueryInfo, "AppNews", session, request);
        map.put("appNewsList", newinfo.getRows());
        return "/web/index.jsp";
    }
    @RequestMapping(value = "/findGoodsByKey",method = RequestMethod.GET)
    @ResponseBody
    public List<WareGoodsInfo> findGoodsByKey(WareGoodsQuery goodsQuery,String more,HttpSession session, HttpServletRequest request){
        Object currentPage = session.getAttribute("goodsCurrentPage")==null?"1":session.getAttribute("goodsCurrentPage");
        Integer goodsCurrentPage=Integer.parseInt(currentPage.toString());
        if(more!=null && more.equals("more")){
            goodsCurrentPage++;
            session.setAttribute("goodsCurrentPage",goodsCurrentPage);
        }
        if(goodsQuery.getMinPrice() == null && goodsQuery.getMaxPrice()==null
                && goodsQuery.getBrandName()==null && goodsQuery.getBrandPinyin()==null
                    && goodsQuery.getTypeName() ==null && more==null){
            QueryInfo queryInfo = new QueryInfo();
            queryInfo.setSort("createTime");
            PageInfo wareGoodsInfo = new QueryController().query(queryInfo, "WareGoodsInfo", session, request);
            System.out.println("wareGoodsInfo:"+wareGoodsInfo.getRows().size());
            //保存下一页的页码
            session.setAttribute("goodsCurrentPage",1);
            return  wareGoodsInfo.getRows();
        }
        //PageHelper.startPage(goodsCurrentPage,3);
        List<WareGoodsInfo> wareGoodsInfo = webGoodsMapper.findGoodsByKey(goodsQuery);
        //保存下一页的页码
        return  wareGoodsInfo;
    }

    @RequestMapping("/newsQueryinfo/{count}/{page}")
    public String NewsQueryinfo(@PathVariable("count") int count, @PathVariable("page") int page, HttpSession session, HttpServletRequest request, ModelMap map) {
        QueryInfo newQueryInfo = new QueryInfo();
        newQueryInfo.setPageSize(count);
        newQueryInfo.setCurrentPage(page);
        newQueryInfo.setSort("createTime");
        PageInfo newinfo = new QueryController().query(newQueryInfo, "AppNews", session, request);
        map.put("appNewsListpage", newinfo.getRows());
        newinfo.setPageSize(count);
        map.put("pageinfo", newinfo);
        return "/web/newsList.jsp";
    }

    @RequestMapping("/newsQueryinfo_hot/{count}/{page}")
    public String NewsQueryinfo_hot(@PathVariable("count") int count, @PathVariable("page") int page, HttpSession session, HttpServletRequest request, ModelMap map) {
        QueryInfo newQueryInfo = new QueryInfo();
        newQueryInfo.setPageSize(count);
        newQueryInfo.setCurrentPage(page);
        newQueryInfo.setSort("readCount");
        PageInfo newinfo = new QueryController().query(newQueryInfo, "AppNews", session, request);
        map.put("appNewsListpage_hot", newinfo.getRows());
        newinfo.setPageSize(count);
        map.put("pageinfo_hot", newinfo);
        return "/web/Hotstate.jsp";
    }

    @RequestMapping("/getnewsInfo_detail/{id}")
    public String getnewsInfo_detail(@PathVariable("id") int id, HttpSession session, HttpServletRequest request, ModelMap map) {
        AppNews appNews = webNewsMapper.selectQueryById(id);
        if (appNews != null) {
            appNews.setReadCount(appNews.getReadCount() + 1);
            webNewsMapper.updateByPrimaryKey(appNews);
            map.put("news", appNews);
        }
        return "/web/introduce.jsp";
    }
    @RequestMapping("/findGoodsDetails")
    public String findGoodsDetails(Integer goodsId,ModelMap map){
        WareGoodsInfo goodsDetails = webGoodsMapper.findGoodsDetails(goodsId);
        map.put("goodsDetails",goodsDetails);
        return "/web/detail.jsp";
    }
}
